package com.ww.springboot.boot.thread.study.base.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * 中断状态(sleep)被打断(.interrupt) ->线程会抛出InterruptedException异常
 * 1.未开始或已结束无法被打断
 * 2.打断后会抛出InterruptedException ->同时会恢复interrupt为false
 *
 * @author wanwei
 * @since 2021-04-17 15:46
 */
public class InterruptDemo1 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                System.out.println("Oh, i am be interrupted.");
            }
        });
        thread.start();
        //short block and make sure thread is started.
        TimeUnit.SECONDS.sleep(3);
        System.out.println(thread.isInterrupted());
        thread.interrupt();
        System.out.println(thread.isInterrupted());
        TimeUnit.SECONDS.sleep(1);
        System.out.println(thread.isInterrupted());
        System.out.println("end~~~~~~~~~");
    }
}
